﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService

@if (MainItems.Count > 0)
{
    <div class="row">
        <div class="col-12 col-md-4 mb-4">
            <CnGalWebSite.Shared.MasaComponent.PC.Home.Documents.NavCard Id="Id" Items="_dataCacheService.DocumentsCatche" IdChanged="IdChanged" />

        </div>
        <div class="col-12 col-md-8">
            <CnGalWebSite.Shared.MasaComponent.PC.Home.Documents.MainGroupCard Items="MainItems" Index="Id" @ref="mainGroupCard" />
        </div>
    </div>
}

@code {
    [Parameter]
    public long Id { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private StringNumber Index { get; set; } = 0;
    private List<DocumentViewModel> MainItems { get; set; } = new List<DocumentViewModel>();
    CnGalWebSite.Shared.MasaComponent.PC.Home.Documents.MainGroupCard mainGroupCard;

    protected override async Task OnInitializedAsync()
    {

        try
        {
            if (_dataCacheService.DocumentsCatche.Count == 0)
            {
                _dataCacheService.DocumentsCatche = await _httpService.GetAsync<List<DocumentViewModel>>("api/home/GetDocuments");
            }
            foreach (var item in _dataCacheService.DocumentsCatche)
            {
                AddItem(item);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取文档列表失败");
        }
    }

    public void AddItem(DocumentViewModel model)
    {
        MainItems.Add(model);
        if(model.Children!=null)
        {
            foreach (var item in model.Children)
            {
                AddItem(item);
            }
        }


    }

    public void IdChanged(long id)
    {
        if (mainGroupCard != null)
        {
            Id = id;
            mainGroupCard.Refresh(id);

        }
    }

}
